package com.wxp.demo07.client.app;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.wxp.demo07.client.R;
import com.wxp.demo07.client.app.client.entity.UserEntity;
import com.wxp.demo07.client.app.client.provider.UserInfoContent;
import com.wxp.demo07.client.util.LogUtils;
import com.wxp.demo07.client.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 1.1 在应用之间共享数据-客户端
 */
public class AppContentProviderClientActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ck_married;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_app_content_provider_client);

        this.setTitle("1.2 在应用之间共享数据-客户端");

        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        ck_married = findViewById(R.id.ck_married);

        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.btn_delete).setOnClickListener(this);
        findViewById(R.id.btn_read).setOnClickListener(this);

    }

    @SuppressLint("Range")
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_save) {
            ContentValues values = new ContentValues();
            values.put(UserInfoContent.USER_NAME, et_name.getText().toString());
            values.put(UserInfoContent.USER_AGE, Integer.parseInt(et_age.getText().toString()));
            values.put(UserInfoContent.USER_HEIGHT, Integer.parseInt(et_height.getText().toString()));
            values.put(UserInfoContent.USER_WEIGHT, Float.parseFloat(et_weight.getText().toString()));
            values.put(UserInfoContent.USER_MARRIED, ck_married.isChecked());
            this.getContentResolver().insert(UserInfoContent.CONTENT_URI, values);
            ToastUtil.show(this, "保存成功");
            return;
        }

        if (v.getId() == R.id.btn_read) {
            Cursor cursor = this.getContentResolver().query(UserInfoContent.CONTENT_URI, null, null, null, null);
            List<UserEntity> userEntityList = new ArrayList<>();
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    UserEntity info = new UserEntity();
                    info.id = cursor.getInt(cursor.getColumnIndex(UserInfoContent._ID));
                    info.name = cursor.getString(cursor.getColumnIndex(UserInfoContent.USER_NAME));
                    info.age = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_AGE));
                    info.height = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_HEIGHT));
                    info.weight = cursor.getFloat(cursor.getColumnIndex(UserInfoContent.USER_WEIGHT));
                    info.married = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_MARRIED)) == 1;
                    LogUtils.debug("用户信息：", info.toString());
                    userEntityList.add(info);
                }
                cursor.close();
            }

            ToastUtil.show(this, "用户信息：" + userEntityList);
            return;
        }

        if (v.getId() == R.id.btn_delete) {
            //单用户删除
            //path请求参数传递：
            //请求地址：content://com.wxp.demo07.app.server.provider.UserInfoProvider/user/2
            //Uri uri = ContentUris.withAppendedId(UserInfoContent.CONTENT_URI, 2);
            //int count = getContentResolver().delete(uri, null, null);

            //多用户删除
            //query参数传递：
            //请求地址：content://com.wxp.demo07.app.server.provider.UserInfoProvider/user
            int count = this.getContentResolver().delete(UserInfoContent.CONTENT_URI, "name=?", new String[]{"wxp"});
            if (count > 0) {
                ToastUtil.show(this, "删除成功");
            }
            return;
        }
    }
}